package _interview150;

import java.util.HashMap;
import java.util.Objects;

/**
 * 290. 单词规律
 */
public class No290 {
    public boolean wordPattern(String pattern, String str) {
        HashMap<Character, String> p2s = new HashMap<>();
        HashMap<String, Character> s2p = new HashMap<>();
        String[] split = str.split(" ");
        if (pattern.length() != split.length) return false;
        for (int i = 0; i < pattern.length(); i++) {
            char p = pattern.charAt(i);
            String s = split[i];
            if (p2s.containsKey(p) && !Objects.equals(p2s.get(p), s)) return false;
            if (s2p.containsKey(s) && !Objects.equals(s2p.get(s), p)) return false;
            p2s.put(p, s);
            s2p.put(s, p);
        }
        return true;
    }
}
